package com.fdhwws.web.servlet.api;

import com.fdhwws.web.domain.ChatRoom;
import com.fdhwws.web.domain.ChatRoomMsg;
import com.fdhwws.web.domain.User;
import com.fdhwws.web.domain.UserFootprints;
import com.fdhwws.web.entity.AjaxResult;
import com.fdhwws.web.service.ChatRoomMsgService;
import com.fdhwws.web.service.UserFootprintsService;
import com.fdhwws.web.service.impl.ChatRoomMsgServiceImpl;
import com.fdhwws.web.service.impl.UserFootprintsServiceImpl;
import com.fdhwws.web.servlet.BaseServlet;
import jakarta.servlet.annotation.WebServlet;

import java.sql.Timestamp;
import java.util.List;

@WebServlet("/api/chatRoomMsg/*")
public class ChartRoomMsgAPIServlet extends BaseServlet {
    private final ChatRoomMsgService chatRoomMsgService = new ChatRoomMsgServiceImpl();
    private final UserFootprintsService userFootprintsService = new UserFootprintsServiceImpl();

    public AjaxResult getChatRoomMsg(HttpUtil httpUtil) throws Exception {
        AjaxResult ajaxResult = verifyUser(httpUtil);
        if (!ajaxResult.isSuccess()) {
            return ajaxResult;
        }
        try {
            ChatRoom jsonBody = httpUtil.getJSONBody(ChatRoom.class);
            AjaxResult chatRoomMsg = chatRoomMsgService.getChatRoomMsg(jsonBody.getId(), jsonBody.getPassword());
            if (ajaxResult.isSuccess()) {
                List<ChatRoomMsg> data = (List<ChatRoomMsg>) chatRoomMsg.data;
                data.forEach(ChatRoomMsg::safeMode);
            }
            return chatRoomMsg;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error("获取聊天室消息失败", null);
    }

    public AjaxResult sendMessage(HttpUtil httpUtil) throws Exception {
        AjaxResult ajaxResult = verifyUser(httpUtil);
        if (!ajaxResult.isSuccess()) {
            return ajaxResult;
        }
        User user = (User) ajaxResult.data;
        AjaxResult result = null;
        try {
            Number roomId = httpUtil.getNumberParameter("roomId");
            String roomPwd = httpUtil.getParameter("roomPwd");
            String message = httpUtil.getParameter("message");
            result = chatRoomMsgService.sendMessage(roomId.intValue(), roomPwd, user, message);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("发送消息失败", null);
        }
        if (result.isSuccess()) {
            List<ChatRoomMsg> data = (List<ChatRoomMsg>) result.data;
            data.forEach(ChatRoomMsg::safeMode);
        }
        return result;
    }

    public AjaxResult enterChatRoomMsg(HttpUtil httpUtil) throws Exception {
        AjaxResult ajaxResult = verifyUser(httpUtil);
        if (!ajaxResult.isSuccess()) {
            return ajaxResult;
        }
        User user = (User) ajaxResult.data;
        AjaxResult result;
        try {
            Number roomId = httpUtil.getNumberParameter("roomId");
            String roomPwd = httpUtil.getParameter("roomPwd");
            User sysUser = new User();
            sysUser.setId(0);
            sysUser.setUsername("系统消息");
            result = chatRoomMsgService.sendMessage(roomId.intValue(), roomPwd, sysUser, "欢迎'" + user.getUsername() + "'进入聊天室");
            AjaxResult userFootprints = userFootprintsService.findUserFootprintsByUidAndRoomId(user.getId(), roomId.intValue());
            if (!userFootprints.isSuccess()) {
                AjaxResult ajaxResult1 = userFootprintsService.insertUserFootprints(new UserFootprints(null, user.getId(), roomId.intValue(), null));
                if (!ajaxResult1.isSuccess()) {
                    System.out.println(ajaxResult1.msg);
                }
            } else {
                UserFootprints data = (UserFootprints) userFootprints.data;
                AjaxResult ajaxResult1 = userFootprintsService.updateUserFootprintsById(new UserFootprints(data.getId(), null, null, new Timestamp(System.currentTimeMillis())));
                if (!ajaxResult1.isSuccess()) {
                    System.out.println(ajaxResult1.msg);
                }
            }
        } catch (Exception e) {
            return AjaxResult.error("进入聊天室失败", null);
        }
        if (result.isSuccess()) {
            List<ChatRoomMsg> data = (List<ChatRoomMsg>) result.data;
            data.forEach(ChatRoomMsg::safeMode);
        }
        return result;
    }
}
